/**
 * 订单变更 - 编辑页面
 * @author miaoyl
 * @date 2019/03/14
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck: false,
            allDisabled: false,
            GWAK063: null,
            GWAS003: null,//客户默认仓库类型
            GWDF024: 1,
            GWDF019: null,
            form: {
                TC003: null,
                TC040: null
            },
            disabled: false,
            rules: {
                GWDFMLEGAL: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDF006: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDF001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                /*GWDF007: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]*/
            },
            selectOptions: {
                crm_gwdf_gwdf009: app.datas_("crm_gwai501_gwax011"),
                crm_gwdf_gwdf007: app.datas_("crm_gwdf_gwdf007")
            }
        },
        cols = [[
            {type: 'checkbox', title: '选择'}
            , {field: 'GWDFSEQ', width: 80, title: '项次'}
            , {field: 'GWDF004', width: 140, title: 'ERP订单项次', event: 'openTD'}
            , {field: 'GWDF011', width: 140, title: '品号', event: 'openGwdf011'}
            , {field: 'GWAML003', width: 140, title: '品名'}
            , {field: 'GWAML004', width: 140, title: '规格'}
            , {
                field: 'GWDF020', width: 80, title: '原仓库类型',
                filterDatas: app.datas_('crm_gwai600_gwas003')
            }
            , {
                field: 'GWDF021', width: 140, title: '新仓库类型',
                filterDatas: app.datas_('crm_gwai600_gwas003')
            }
            , {field: 'GWDF022', width: 100, title: '原发货仓库'}
            , {field: 'GWDF023', width: 100, title: '新发货仓库', event: 'openCk'}
            , {field: 'GWDF035', width: 100, title: '订单折扣率'}
            , {field: 'GWDF036', width: 100, title: '折扣率', edit: 'text'}
            , {field: 'GWDF013', width: 100, title: '原订单数量'}
            , {field: 'GWDF014', width: 100, title: '新订单数量', edit: 'text'}
            , {field: 'GWDF015', width: 100, title: '发货数量'}
            , {field: 'GWDF016', width: 100, title: '原单价'}
            , {field: 'GWDF017', width: 100, title: '新单价', edit: 'text'}
            , {field: 'GWDF025', width: 100, title: '原金额'}
            , {field: 'GWDFNEW', width: 100, title: '新税前金额'}
            , {field: 'GWDF029', width: 100, title: '新税额'}
            , {field: 'GWDF026', width: 100, title: '新含税金额'}
            , {field: 'GWDF018', width: 100, title: '新本币单价', hide: true}
            , {field: 'GWDF028', width: 100, title: '新本币金额', hide: true}
            , {field: 'GWDF030', width: 100, title: '新本币税额', hide: true}
        ]],
        callObj,
        table,
        gwai770Table,layOpenIndex
    ;

    var Gwai770Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwai770_edit',
                    _loadTplsFull: [{
                        dom: 'common_open_window', tpl: 'common/_open_window' //0
                    }
                    // , {
                    //     dom: 'common_open_kh', tpl: 'common/_open_kh' //1
                    // }, {
                    //     dom: 'common_open_coptc_h', tpl: 'common/_open_coptc_h'//2
                    // }, {
                    //     dom: 'common_open_coptd', tpl: 'common/_open_coptd' //3
                    // }, {
                    //     dom: 'common_open_ck', tpl: 'common/_open_ck' //4
                    // }, {
                    //     dom: 'common_open_gwda003', tpl: 'common/_open_gwda003' //5
                    // },
                    ]
                }, win);
            },

            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwai770Table = table.render({
                        elem: app.getByid(doc, 'gwai770_edit_table'),
                        id: 'gwai770_edit_table',
                        cols: cols,
                        height: '250',
                        page: false
                    });

                    //监听单元格编辑
                    table.on('edit(gwai770_edit_table)', function (obj) {
                        if (obj.field == 'GWDF014') {
                            var data = obj.data;
                            if (!(/^[0-9]+.?[0-9]*$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    GWDF014: ''
                                });
                                app.showError('数量必须为数字！');
                                return;
                            }
                            if (parseInt(obj.value) < parseInt(data.GWDF015)) {
                                app.showWarning("新订单数量不能小于发货数量");
                                var GWDF026 = math.mul(data.GWDF015, data.GWDF017);
                                var t1 = math.sub(1, math.div(1, math.add(1, vueData.form.GWDF010)));
                                var t2 = math.div(1, math.add(1, vueData.form.GWDF010));
                                var GWDFNEW = math.mul(GWDF026, t2).toFixed(2);
                                var GWDF030 = math.mul(math.sub(GWDF026, GWDFNEW), data.GWDF024);
                                obj.updateOfEdit({
                                    'GWDF014': data.GWDF015,
                                    'GWDFNEW': GWDFNEW,
                                    'GWDF029': math.sub(GWDF026, GWDFNEW),
                                    'GWDF026': GWDF026,
                                    'GWDF028': math.mul(GWDF026, data.GWDF024),
                                    'GWDF030': GWDF030
                                });
                                return;
                            } else {
                                var GWDF026 = math.mul(obj.value, data.GWDF017);
                                var t1 = math.sub(1, math.div(1, math.add(1, vueData.form.GWDF010)));
                                var t2 = math.div(1, math.add(1, vueData.form.GWDF010));
                                var GWDFNEW = math.mul(GWDF026, t2).toFixed(2);
                                var GWDF030 = math.mul(math.sub(GWDF026, GWDFNEW), data.GWDF024);
                                obj.updateOfEdit({
                                    'GWDF014': obj.value,
                                    'GWDFNEW': GWDFNEW,
                                    'GWDF029': math.sub(GWDF026, GWDFNEW),
                                    'GWDF026': GWDF026,
                                    'GWDF028': math.mul(GWDF026, data.GWDF024),
                                    'GWDF030': GWDF030
                                });
                            }
                        } else if (obj.field == 'GWDF017') {
                            if (!(/^[0-9]+.?[0-9]*$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    GWDF017: ''
                                });
                                app.showError('单价必须为数字！');
                                return;
                            }
                            var data = obj.data;
                            if (parseInt(obj.value) < 0) {
                                app.showWarning("价格不能小于0！");
                                var GWDF026 = math.mul(data.GWDF015, data.GWDF017);
                                var t1 = math.sub(1, math.div(1, math.add(1, vueData.form.GWDF010)));
                                var t2 = math.div(1, math.add(1, vueData.form.GWDF010));
                                var GWDFNEW = math.mul(GWDF026, t2).toFixed(2);
                                obj.updateOfEdit({
                                    'GWDF017': data.GWDF016,
                                    'GWDF018': math.mul(data.GWDF016, data.GWDF024),
                                    'GWDFNEW': GWDFNEW,
                                    'GWDF029': math.sub(GWDF026, GWDFNEW),
                                    'GWDF026': GWDF026,
                                    'GWDF028': math.mul(GWDF026, data.GWDF024),
                                    'GWDF030': math.mul(math.sub(GWDF026, GWDFNEW), data.GWDF024)
                                });
                                return;
                            } else {
                                if (data.GWDF014 != null && data.GWDF014 != '') {
                                    var GWDF026 = math.mul(obj.value, data.GWDF014);
                                    var t1 = math.sub(1, math.div(1, math.add(1, vueData.form.GWDF010)));
                                    var t2 = math.div(1, math.add(1, vueData.form.GWDF010));
                                    var GWDFNEW = math.mul(GWDF026, t2).toFixed(2);
                                    var GWDF036 = math.div(obj.value, data.GWDF016).toFixed(6);
                                    obj.updateOfEdit({
                                        'GWDF036': GWDF036,
                                        'GWDF017': obj.value,
                                        'GWDF018': math.mul(obj.value, data.GWDF024),
                                        'GWDFNEW': GWDFNEW,
                                        'GWDF029': math.sub(GWDF026, GWDFNEW),
                                        'GWDF026': GWDF026,
                                        'GWDF028': math.mul(GWDF026, data.GWDF024),
                                        'GWDF030': math.mul(math.sub(GWDF026, GWDFNEW), data.GWDF024)
                                    });
                                }
                            }
                        } else if (obj.field == 'GWDF036') {
                            if (!(/^[0-9]+.?[0-9]*$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    GWDF036: ''
                                });
                                app.showError('折扣率必须为数字！');
                                return;
                            }
                            var data = obj.data;
                            var GWDF017 = math.mul(obj.value, data.GWDF016);
                            var GWDF026 = 0;
                            var GWDFNEW = 0;
                            if (data.GWDF014 != null && data.GWDF014 != '') {
                                GWDF026 = math.mul(GWDF017, data.GWDF014);
                                var t1 = math.sub(1, math.div(1, math.add(1, vueData.form.GWDF010)));
                                var t2 = math.div(1, math.add(1, vueData.form.GWDF010));
                                GWDFNEW = math.mul(GWDF026, t2).toFixed(2);
                            }
                            obj.updateOfEdit({
                                'GWDF036': obj.value,
                                'GWDF017': GWDF017,
                                'GWDF018': math.mul(GWDF017, data.GWDF024),
                                'GWDFNEW': GWDFNEW,
                                'GWDF029': math.sub(GWDF026, GWDFNEW),
                                'GWDF026': GWDF026,
                                'GWDF028': math.mul(GWDF026, data.GWDF024),
                                'GWDF030': math.mul(math.sub(GWDF026, GWDFNEW), data.GWDF024)
                            });
                        }
                    });

                    table.on('tool(gwai770_edit_table)', function (obj) {
                        if (obj.event == 'openTD') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_coptd',
                                        columnList: [
                                            {label: 'ERP订单项次', prop: 'TD003', width: '150'},
                                            {label: '品号', prop: 'TD004', width: '150'},
                                            {label: '品名', prop: 'TD005', width: '150'},
                                            {label: '规格', prop: 'TD006', width: '150'},
                                            {label: '仓库类型', prop: 'GWCS112_NAME', width: '150'},
                                            {label: '仓库', prop: 'TD007', width: '150'},
                                            {label: '折扣率', prop: 'TD026', width: '150'},
                                            {label: '订单数量', prop: 'TD008', width: '150'},
                                            {label: '发货数量', prop: 'TH008_SUM', width: '150'},
                                            {label: '单价', prop: 'TD011', width: '150'}
                                        ],
                                        searchParam:{
                                            mlegal: vueData.form.GWDFMLEGAL,
                                            TD002: vueData.form.GWDF003
                                        }
                                    },
                                    cb: function (data) {
                                        var t2 = math.div(1, math.add(1, vueData.form.GWDF010));
                                        var GWDFNEW = math.mul(data.TD012, t2).toFixed(2);
                                        obj.update({
                                            GWDF004: data.TD003,
                                            GWDF005: (data.TD048).trim(),
                                            GWDF011: data.TD004,
                                            GWAML003: data.TD005,
                                            GWAML004: data.TD006,
                                            GWDF020: data.GWCS112,
                                            GWDF021: data.GWCS112,
                                            GWDF022: data.TD007,
                                            GWDF023: data.TD007,
                                            GWDF013: data.TD008,
                                            GWDF014: data.TD008,//默认订单数量
                                            GWDF015: data.TH008_SUM,
                                            GWDF016: data.TD011,
                                            GWDF017: data.TD011,
                                            GWDF018: math.mul(data.TD011, data.TC009),
                                            GWDF025: data.TD012,
                                            GWDF026: data.TD012,
                                            GWDF028: math.mul(data.TD012, data.TC009),
                                            GWDF029: math.sub(data.TD012, GWDFNEW),
                                            GWDFNEW: GWDFNEW,
                                            GWDF030: math.mul(math.sub(data.TD012, GWDFNEW), data.TC009),
                                            GWDF012: data.TD010,//销售单位
                                            GWDF019: data.TC008,//交易币别
                                            GWDF024: data.TC009,//汇率
                                            GWDF027: math.mul(data.TD012, data.TC009),//订单本币金额
                                            GWDF035: data.TD026,
                                            GWDF036: data.TD026
                                        })
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择ERP数据',
                                    area: ['1000px', '500px']
                                }
                            });
                        } else if (obj.event == 'openCk') {
                            var tableData = obj.data;
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_gwas001',
                                        columnList: [
                                            {label: '仓库编号', prop: 'GWAS001', width: '150'},
                                            {label: '仓库名称', prop: 'GWASL003', width: '150'},
                                            {label: '助记码', prop: 'GWASL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        //仓库<>订单仓库且客户仓库类型==订单仓库类型==1，折扣率=订单折扣率*（1+GWAK063/100）data.GWAS003 != tableData.GWDF020 && vueData.GWAS003 == tableData.GWDF020 && tableData.GWDF020 == '1'
                                        var GWDF036 = tableData.GWDF035;
                                        if (data.GWAS003 != vueData.GWAS003) {
                                            GWDF036 = math.mul(tableData.GWDF035, math.add(1,math.div(vueData.GWAK063, 100))).toFixed(4);//折扣率
                                        }
                                        var GWDF017 = math.mul(tableData.GWDF016, GWDF036).toFixed(6);//原币单价
                                        var GWDF018 = math.mul(GWDF017, tableData.GWDF024);//本币单价
                                        var GWDF026 = math.mul(GWDF017, tableData.GWDF014);//原币金额
                                        var GWDF028 = math.mul(GWDF018, tableData.GWDF014);//本币金额
                                        var t = math.div(vueData.form.GWDF010, math.add(1, vueData.form.GWDF010));
                                        var GWDF029 = math.mul(GWDF026, t).toFixed(2);//原币税额
                                        var GWDF030 = math.mul(GWDF028, t).toFixed(2);//本币税额
                                        var GWDFNEW = math.sub(GWDF026, GWDF029);
                                        obj.update({
                                            GWDF023: data.GWAS001,
                                            GWDF021: data.GWAS003,
                                            GWDF036: GWDF036,
                                            GWDF017: GWDF017,
                                            GWDF018: GWDF018,
                                            GWDF028: GWDF028,
                                            GWDF026: GWDF026,
                                            GWDF029: GWDF029,
                                            GWDF030: GWDF030,
                                            GWDFNEW: GWDFNEW
                                        })

                                        // else {
                                        //     obj.update({
                                        //         GWDF023: data.GWAS001,
                                        //         GWDF021: data.GWAS003
                                        //     })
                                        // }
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择仓库',
                                    area: ['600px', '470px']
                                }
                            });
                        } else if (obj.event == 'openGwdf011') {//未选择订单号，产品可以开窗,开产品经营范围内的商品
                            var tableData = obj.data;
                            if (tableData.GWDF004 == null || tableData.GWDF004 == '') {
                                var keyArray=[];
                                var list = gwai770Table.getDatas();
                                for(var i =0;i<list.length;i++){
                                    keyArray.push(
                                        list[i]['GWDF011']
                                    );
                                }
                                var keyArrayStr=keyArray.join(',');
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        multiple: true,
                                        config: {
                                            method: 'cq_gwam001_3',
                                            searchParam: {
                                                GWCS010: vueData.form.GWDF006, //客户
                                                keyArrayStr:keyArrayStr
                                            },
                                            columnList: [
                                                { label: '产品编号', prop: 'GWDA003', width: '150'},
                                                { label: '品名', prop: 'GWAML003', width: '150'},
                                                { label: '规格', prop: 'GWAML004', width: '150'},
                                                { label: '单位', prop: 'GWAM008', width: '150',unshow:true}
                                            ],
                                            dataList: [],
                                            key: 'GWDA003',
                                        },
                                        cb: function (data) {
                                            var select_data = [];
                                            var select = [];
                                            for (var i = 0; i < data.length; i++) {
                                                select_data.push({
                                                    GWDC016: vueData.form.GWDF006,      //客户编号
                                                    GWDC007: '2',                   //对象类型：2.渠道商
                                                    GWDC018: data[i].GWDA003,      //产品
                                                    GWDC008: vueData.form.TC003 //单据日期
                                                });
                                            }
                                            app.ajax({//获取结算价
                                                xurl: 'crm/gwai770/getPrice',
                                                xdata: select_data,
                                                toJsonStr: true,
                                                cb: function (result) {
                                                    if (result.ok) {
                                                        if (result.data.data != "") {
                                                            var data1 = result.data.data;
                                                            var count = obj.data.GWDFSEQ;
                                                            var listData = gwai770Table.getDatas();
                                                            listData.removeByMap({
                                                                GWDFSEQ: obj.data.GWDFSEQ
                                                            });
                                                            for (var j = 0; j < data1.length; j++) {
                                                                listData.push({
                                                                    GWDFSEQ: count,
                                                                    GWDF004: '',
                                                                    GWDF011: data1[j].GWAM001,
                                                                    GWAML003: data1[j].GWAML003,
                                                                    GWAML004: data1[j].GWAML004,
                                                                    GWDF020: '',
                                                                    GWDF021: data1[j].GWAS003,
                                                                    GWDF022: '',
                                                                    GWDF023: data1[j].GWAK058,
                                                                    GWDF035: 1,
                                                                    GWDF036: 1,
                                                                    GWDF013: '',
                                                                    GWDF012: data1[j].GWAM008,
                                                                    GWDF014: '',
                                                                    GWDF015: 0,
                                                                    GWDF016: data1[j].GWDB016,
                                                                    GWDF017: data1[j].GWDB016,
                                                                    GWDF025: '',
                                                                    GWDF026: '',
                                                                    GWDF029: '',
                                                                    GWDF030: '',
                                                                    GWDF019: self.GWDF019,
                                                                    GWDF024: self.GWDF024
                                                                });
                                                                select.push(data1[j].GWAM001);
                                                                count++;
                                                            }
                                                            self.tableReload(listData);
                                                        }
                                                        var unselect = [];
                                                        var arr2 = [];
                                                        select_data.forEach(function (v) {
                                                            arr2.push(v.GWDF011);
                                                        });
                                                        select.concat(arr2).filter(function (item) {
                                                            if (!select.includes(item) || !arr2.includes(item)) {
                                                                unselect.push(item);
                                                            }
                                                        });
                                                        if (result.data.data.length != data.length) {
                                                            var text = '';
                                                            unselect.forEach(function (v) {
                                                                text = text + v + ',';
                                                            });
                                                            text = text.substring(0, text.length - 1);
                                                            app.showMessage('选择的商品' + text + '未设置结算价请检查!');
                                                            return;
                                                        }
                                                    }
                                                }
                                            });
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择产品信息',
                                        area: ['600px', '430px']
                                    }
                                });
                            }
                        }
                    });
                });
            },

            tableReload: function (listData) {
                gwai770Table.reload({data: listData});
            },

            addAllData: function (listData1) {
                app.ajax({
                    xurl: 'crm/gwai770/add',
                    xdata: listData1,
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwai770').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            editAllData: function (listData1) {
                app.ajax({
                    xurl: 'crm/gwai770/edit',
                    xdata: {list1: listData1, GWDFDOCNO: vueData.form.GWDFDOCNO},
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwai770').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            _call: function (tagData) {
                layOpenIndex = tagData.layOpenIndex;
                vueData.GWDF024 = 1;
                vueData.selectOptions.crm_gwdc_gwdd003 = [];
                if (tagData.isCheck) {
                    vueData.isCheck = true;
                } else {
                    vueData.isCheck = false;
                }
                if (tagData.is_add) {
                    vueData.isAdd = true;
                    vueData.disabled = false;
                    vueData.allDisabled = false;
                    app.ajax({
                        xurl: 'crm/gwai770/getAddData',
                        cb: function (result) {
                            if (result.ok) {
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: {
                                        GWDFDOCNO: result.data.data.GWDFDOCNO,
                                        GWDF009: 'N'
                                    },
                                    reset: true
                                });
                                var listData = [];
                                self.tableReload(listData);
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    vueData.disabled = true;
                    vueData.allDisabled = true;
                    callObj = tagData.obj;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj.data,
                        reset: true
                    });
                    app.ajax({
                        xurl: 'crm/gwai770/getGwdfInfo',
                        xdata: {GWDFDOCNO: callObj.data.GWDFDOCNO},
                        loading: true,
                        cb: function (result) {
                            if (result.ok) {
                                var listData = result.data.data;
                                if (listData.length > 0) {
                                    self.GWDF019 = listData[0].TC008;
                                    self.GWDF024 = listData[0].TC009;//汇率
                                }
                                self.tableReload(listData);
                            }
                        }
                    });
                    app.ajax({
                        xurl: 'crm/gwai770/getGwak063',
                        xdata: {GWAK001: callObj.data.GWDF006},
                        loading: true,
                        cb: function (result) {
                            if (result.ok) {
                                vueData.GWAK063 = result.data.data.GWAK063;
                                vueData.GWAS003 = result.data.data.GWAS003;
                            }
                        }
                    });
                }
            },

            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['gwdf_t'], vueData.form);
                var vue = new Vue({
                    el: '#gwai770_edit',
                    data: vueData,
                    watch: {
                        'form.GWDF009': function () {
                            if (vueData.form.GWDF009 == 'Y') {
                                if (vueData.form.GWDFMLEGAL == '' || vueData.form.GWDFMLEGAL == null) {
                                    vueData.form.GWDF009 = 'N';
                                    app.showWarning("请先选择法人！");
                                    return;
                                }
                                if (vueData.form.GWDF003 == '' || vueData.form.GWDF003 == null) {
                                    vueData.form.GWDF009 = 'N';
                                    app.showWarning("请先选择分销订单或ERP订单！");
                                    return;
                                }
                                app.ajax({
                                    xurl: 'crm/gwai770/getCOPTDData',
                                    xdata: {
                                        mlegal: vueData.form.GWDFMLEGAL,
                                        TD002: vueData.form.GWDF003
                                    },
                                    loading: true,
                                    cb: function (result) {
                                        var listQData = result.data.data;
                                        var listData = gwai770Table.getDatas();
                                        var count = 0;
                                        if (listData.length > 0) {
                                            count = parseInt(listData[listData.length - 1].GWDFSEQ);
                                        }
                                        for (var i = 0; i < listQData.length; i++) {
                                            var GWDF026 = math.mul(listQData[i].TD011, listQData[i].TH008_SUM);
                                            var t1 = math.sub(1, math.div(1, math.add(1, vueData.form.GWDF010)));
                                            var t2 = math.div(1, math.add(1, vueData.form.GWDF010));
                                            var GWDFNEW = math.mul(GWDF026, t2).toFixed(2);
                                            listData.push({
                                                GWDFSEQ: count + 1,
                                                GWDF004: listQData[i].TD003,
                                                GWDF005: (listQData[i].TD048).trim(),
                                                GWDF011: listQData[i].TD004,
                                                GWAML003: listQData[i].TD005,
                                                GWAML004: listQData[i].TD006,
                                                GWDF020: listQData[i].GWCS112,
                                                GWDF021: listQData[i].GWCS112,
                                                GWDF022: listQData[i].TD007,
                                                GWDF023: listQData[i].TD007,
                                                GWDF013: listQData[i].TD008,
                                                GWDF014: listQData[i].TH008_SUM,//整单默认发货数量
                                                GWDF015: listQData[i].TH008_SUM,
                                                GWDF016: listQData[i].TD011,
                                                GWDF017: listQData[i].TD011,
                                                GWDF018: math.mul(listQData[i].TD011, listQData[i].TC009),
                                                GWDF025: listQData[i].TD012,
                                                GWDF026: GWDF026,
                                                GWDF028: math.mul(GWDF026, listQData[i].TC009),
                                                GWDF029: math.sub(GWDF026, GWDFNEW),
                                                GWDF030: math.mul(math.sub(GWDF026, GWDFNEW), listQData[i].TC009),
                                                GWDFNEW: GWDFNEW,
                                                GWDF035: listQData[i].TD026,//订单折扣率
                                                GWDF036: listQData[i].TD026,//折扣率
                                                GWDF012: listQData[i].TD010,//销售单位
                                                GWDF019: listQData[i].TC008,//交易币别
                                                GWDF024: listQData[i].TC009,//汇率
                                                GWDF027: math.mul(listQData[i].TD012, listQData[i].TC009)//订单本币金额
                                            });
                                            count++;
                                        }
                                        vueData.allDisabled = true;
                                        self.tableReload(listData);
                                    }
                                });
                            }
                        },
                        'form.GWDF003': function () {
                            if (vueData.form.GWDF003 != '' && vueData.form.GWDF003 != null) {
                                app.ajax({
                                    xurl: 'crm/gwai770/getCOPTDData',
                                    xdata: {
                                        mlegal: vueData.form.GWDFMLEGAL,
                                        TD002: vueData.form.GWDF003
                                    },
                                    cb: function (result) {
                                        var listQData = result.data.data;
                                        if (listQData.length > 0) {
                                            self.GWDF019 = listQData[0].TC008;
                                            self.GWDF024 = listQData[0].TC009;//汇率
                                        }
                                    }
                                });
                            }
                        }
                    },
                    methods: {
                        addData: function () {
                            if (vueData.form.GWDF003 == '' || vueData.form.GWDF003 == null) {
                                app.showWarning("请先选择分销订单或ERP订单！");
                                return;
                            }
                            vueData.allDisabled = true;
                            var listData = gwai770Table.getDatas();
                            var count = 0;
                            if (listData.length > 0) {
                                count = parseInt(listData[listData.length - 1].GWDFSEQ);
                            }
                            listData.push({
                                GWDFSEQ: count + 1,
                                GWDF004: '',
                                GWDF011: '',
                                GWAML003: '',
                                GWAML004: '',
                                GWDF020: '',
                                GWDF021: '',
                                GWDF022: '',
                                GWDF023: '',
                                GWDF013: '',
                                GWDF014: '',
                                GWDF015: '',
                                GWDF016: '',
                                GWDF017: '',
                                GWDF025: '',
                                GWDF026: '',
                                GWDF029: '',
                                GWDF030: '',
                                GWDF024: self.GWDF024,
                                GWDF019: self.GWDF019
                            });
                            self.tableReload(listData);
                        },
                        cancle: function () {
                            layer.close(layOpenIndex);
                        },
                        delData: function () {
                            var list = gwai770Table.getDatas();
                            var checkStatus = table.checkStatus('gwai770_edit_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWDFSEQ: v.GWDFSEQ
                                    });
                                });
                                var listData = list;
                                if (listData.length == 0) {
                                    vueData.allDisabled = false;
                                    vueData.form.GWDF009 = 'N';
                                }
                                self.tableReload(listData);
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var listData_1 = gwai770Table.getDatas();
                                    if (listData_1.length == 0) {
                                        app.showError("单身内容不可为空！");
                                        return;
                                    }
                                    for (var i = 0; i < listData_1.length; i++) {
                                        var v = listData_1[i];
                                        if (v.GWDF014 == '' || v.GWDF014 == null || v.GWDF014 < v.GWDF015) {
                                            app.showWarning('新数量不允许为空且不小于销货数量!');
                                            return;
                                        }
                                        if (v.GWDF017 == '' || v.GWDF017 == null) {
                                            app.showWarning('单价不允许为空！');
                                            return;
                                        }
                                    }
                                    listData_1.addMap(vueData.form, false);
                                    if (vueData.isAdd) {
                                        self.addAllData(listData_1);
                                    } else {
                                        self.editAllData(listData_1);
                                    }
                                }
                            });
                        },
                        openGwdfmlegal: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_gwde003',
                                            columnList: [
                                            {label: '选项值', prop: 'GWDE003', width: '150'},
                                            {label: '选项名称', prop: 'GWDE004', width: '150'}
                                        ],
                                        searchParam:{
                                            GWDE002: 'ERP账套'
                                        }
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDFMLEGAL = data.GWDE003;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择ERP营运中心',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openGwdf006: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_gwak001',
                                        columnList: [
                                            {label: '客户编号', prop: 'GWAK001', width: '150'},
                                            {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                            {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDF006 = data.GWAK001;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择客户',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openCOPTC: function () {
                            if (vueData.form.GWDFMLEGAL == '' || vueData.form.GWDFMLEGAL == null) {
                                app.showWarning("请先选择法人！");
                                return;
                            }
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_coptc',
                                        columnList: [
                                            {label: 'ERP订单编号', prop: 'TC002', width: '150'},
                                            {label: '分销订单编号', prop: 'UDF11', width: '150'},
                                            {label: '客户编号', prop: 'TC004', width: '150'},
                                            {label: '订单日期', prop: 'TC003', width: '150'}
                                        ],
                                        searchParam:{
                                            TC004: vueData.form.GWDF006,
                                            mlegal: vueData.form.GWDFMLEGAL
                                        }
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDF006 = data.TC004;
                                        vueData.form.GWDF003 = data.TC002;
                                        vueData.form.GWDF001 = data.UDF11;
                                        // vueData.form.GWDF007 = data.TC200;
                                        vueData.form.GWDF008 = data.TC003;
                                        vueData.form.GWDF010 = data.TC041;
                                        vueData.form.TC003 = data.TC003;
                                        vueData.form.TC040 = data.TC040;
                                        app.ajax({
                                            xurl: 'crm/gwai770/getGwdf002',
                                            xdata: {GWDF003: data.TC002},
                                            cb: function (result) {
                                                vueData.form.GWDF002 = result.data.data.GWDF002;
                                            }
                                        });
                                        app.ajax({
                                            xurl: 'crm/gwai770/getGwak063',
                                            xdata: {GWAK001: data.TC004},
                                            cb: function (result) {
                                                vueData.GWAK063 = result.data.data.GWAK063;
                                                vueData.GWAS003 = result.data.data.GWAS003;
                                            }
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择订单',
                                    area: ['700px', '470px']
                                }
                            });
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwai770_edit');
                    }
                });

                self.initTable();
            },
            _lazy: function () {
                this._super();
            }
        })
    ;

    return new Gwai770Edit();

})
;
